﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Net.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thinger.AutomaticStoreMotionControlLib
{
    public partial class HeadPanel : Panel
    {
        public HeadPanel()
        {
            InitializeComponent();

            //设定组件类界面重绘的参数初始化
            base.SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }

        #region 属性
        private string _titleText = "系统控制";
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("设置或获取标题文本")]
        public string TitleText
        {
            get { return _titleText; }
            set
            {
                _titleText = value;
                this.Invalidate();
            }
        }

        private Color _themeColor = Color.FromArgb(2, 69, 163);
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("设置或获取标题文本背景色")]
        public Color ThemeColor
        {
            get { return _themeColor; }
            set
            {
                _themeColor = value;
                this.Invalidate();
            }
        }

        private Color _themeForeColor = Color.White;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("设置或获取标题文本前景色")]
        public Color ThemeForeColor
        {
            get { return _themeForeColor; }
            set
            {
                _themeForeColor = value;
                this.Invalidate();
            }
        }

        private int _headHeight = 40;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("设置或获取标题栏高度")]
        public int HeadHeight
        {
            get { return _headHeight; }
            set
            {
                _headHeight = value;
                this.Invalidate();
            }
        }

        private Color _borderColor = Color.Gray;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("设置或获取边框颜色")]
        public Color BorderColor
        {
            get { return _borderColor; }
            set
            {
                _borderColor = value;
                this.Invalidate();
            }
        }

        private float _linearGradientRate = 0.4F;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("设置或获取背景色渐变系数")]
        public float LinearGradientRate
        {
            get { return _linearGradientRate; }
            set
            {
                _linearGradientRate = value;
                this.Invalidate();
            }
        }

        private StringFormat _sf = new StringFormat()
        {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Center
        };
        private ContentAlignment _textAlig = ContentAlignment.MiddleCenter;
        [Browsable(true)]
        [Category("自定义属性")]
        [Description("设置或获取标题文本位置")]
        public ContentAlignment TextAlig
        {
            get { return _textAlig; }
            set
            {
                _textAlig = value;
                switch (_textAlig)
                {
                    case ContentAlignment.TopLeft:
                        _sf.Alignment = StringAlignment.Near;
                        _sf.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.TopCenter:
                        _sf.Alignment = StringAlignment.Center;
                        _sf.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.TopRight:
                        _sf.Alignment = StringAlignment.Far;
                        _sf.LineAlignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.MiddleLeft:
                        _sf.Alignment = StringAlignment.Near;
                        _sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.MiddleCenter:
                        _sf.Alignment = StringAlignment.Center;
                        _sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.MiddleRight:
                        _sf.Alignment = StringAlignment.Far;
                        _sf.LineAlignment = StringAlignment.Center;
                        break;
                    case ContentAlignment.BottomLeft:
                        _sf.Alignment = StringAlignment.Near;
                        _sf.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.BottomCenter:
                        _sf.Alignment = StringAlignment.Center;
                        _sf.LineAlignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.BottomRight:
                        _sf.Alignment = StringAlignment.Far;
                        _sf.LineAlignment = StringAlignment.Far;
                        break;
                }
                this.Invalidate();
            }
        }
        #endregion

        #region 方法重新 OnPaint
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            //画布
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            //创建矩形
            Rectangle rectangle = new Rectangle(0, 0, this.Width, this.HeadHeight);

            //创建画刷
            LinearGradientBrush linearGradientBrush = new LinearGradientBrush(rectangle, ChangeColor(ThemeColor, LinearGradientRate), ThemeColor, 0.0f);
            //绘制
            g.FillRectangle(linearGradientBrush, rectangle);

            //文字
            SolidBrush solidBrush = new SolidBrush(ThemeForeColor);
            g.DrawString(TitleText, Font, solidBrush, rectangle, _sf);

            //绘制边框
            g.DrawRectangle(new Pen(BorderColor), new Rectangle(0, 0, this.Width-1, this.Height-1));
            g.DrawLine(new Pen(BorderColor), 0, HeadHeight-1, this.Width, HeadHeight-1);
        }

        private Color ChangeColor(Color color, float correctionFactor)
        {
            if (correctionFactor > 1.0f) correctionFactor = 1.0f;
            if (correctionFactor < -1.0f) correctionFactor = -1.0f;

            float red = (float)color.R;
            float green = (float)color.G;
            float blue = (float)color.B;

            if (correctionFactor < 0)
            {
                correctionFactor += 1.0f;
                red *= correctionFactor;
                green *= correctionFactor;
                blue *= correctionFactor;
            }
            else
            {
                red = (255.0f - red) * correctionFactor + red;
                green = (255.0f - green) * correctionFactor + green;
                blue = (255.0f - blue) * correctionFactor + blue;
            }

            if (red < 0.0f) red = 0.0f;
            if (red > 255.0f) red = 255.0f;
            if (green < 0.0f) green = 0.0f;
            if (green > 255.0f) green = 255.0f;
            if (blue < 0.0f) blue = 0.0f;
            if (blue > 255.0f) blue = 255.0f;

            return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
        }

        #endregion


    }
}
